//
//  ProductTabView.m
//  quickBatchManager
//
//  Created by 王定波 on 15/10/16.
//  Copyright © 2015年 jack. All rights reserved.
//

#import "ProductTabView.h"

@implementation ProductTabView

/*
 // Only override drawRect: if you perform custom drawing.
 // An empty implementation adversely affects performance during animation.
 - (void)drawRect:(CGRect)rect {
 // Drawing code
 }
 */

-(instancetype)initWithFrame:(CGRect)frame
{
    if(self=[super initWithFrame:frame])
    {
        [self initUI];
    }
    return self;
}


-(instancetype)init
{
    if(self=[super init])
    {
        [self initUI];
    }
    return self;
}

-(instancetype)initWithCoder:(NSCoder *)aDecoder
{
    if(self=[super initWithCoder:aDecoder])
    {
        [self initUI];
    }
    return self;
}

-(void) initUI
{
    
    self.backgroundColor=[UIColor colorWithWhite:0.60 alpha:1];
    self.layer.borderWidth=1;
    self.layer.borderColor=[UIColor colorWithWhite:0.60 alpha:1].CGColor;
    
    _mainTableView=[[UIView alloc] init];
    _mainTableView.translatesAutoresizingMaskIntoConstraints = NO;
    
    [self addSubview:_mainTableView];
    
    
    _productModelLabel=[[UILabel alloc] init];
    _productModelLabel.translatesAutoresizingMaskIntoConstraints = NO;
    _productModelLabel.backgroundColor=[UIColor colorWithWhite:0.73 alpha:1];
    _productModelLabel.textColor=[UIColor whiteColor];
    [self addSubview:_productModelLabel];
    
    _productTotalLabel=[[UILabel alloc] init];
    _productTotalLabel.translatesAutoresizingMaskIntoConstraints = NO;
    _productTotalLabel.backgroundColor=[UIColor colorWithWhite:0.96 alpha:1];
    _productTotalLabel.textAlignment=NSTextAlignmentCenter;
    _productTotalLabel.font=[UIFont systemFontOfSize:13];
    _productTotalLabel.textColor=[UIColor colorWithWhite:0.46 alpha:1];
    [self addSubview:_productTotalLabel];
    
    
    
    
    [self setEdge:self firstView:_mainTableView secondView:self attr:NSLayoutAttributeLeft constant:0];
    [self setEdge:self firstView:_mainTableView secondView:self attr:NSLayoutAttributeRight constant:0];
    
    [self setEdge:self firstView:_productModelLabel secondView:self attr:NSLayoutAttributeLeft constant:0];
    [self setEdge:self firstView:_productModelLabel secondView:self attr:NSLayoutAttributeRight constant:0];
    [self setEdge:self firstView:_productModelLabel secondView:self attr:NSLayoutAttributeTop constant:0];
    
    [self setEdge:self firstView:_productTotalLabel secondView:self attr:NSLayoutAttributeLeft constant:0];
    [self setEdge:self firstView:_productTotalLabel secondView:self attr:NSLayoutAttributeRight constant:0];
    [self setEdge:self firstView:_productTotalLabel secondView:self attr:NSLayoutAttributeBottom constant:0];
    
    
    [self addConstraint:[NSLayoutConstraint constraintWithItem:_productModelLabel attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:_mainTableView attribute:NSLayoutAttributeTop multiplier:1.0 constant:0]];
    
    
    [self addConstraint:[NSLayoutConstraint constraintWithItem:_mainTableView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:_productTotalLabel attribute:NSLayoutAttributeTop multiplier:1.0 constant:-1]];
    
    [self setEdge:self firstView:_productModelLabel secondView:_productTotalLabel attr:NSLayoutAttributeHeight constant:0];
}


-(void)setData:(NSArray*)data
{
    for (UIView *view in _mainTableView.subviews) {
        [view removeFromSuperview];
    }
    
    for (int i=0; i<data.count; i++) {
        NSArray *rowData=data[i];
        for (int j=0; j<rowData.count; j++) {
            UILabel *title=[[UILabel alloc] init];
            title.translatesAutoresizingMaskIntoConstraints = NO;
            title.text=[rowData objectAtIndex:j];
            NSLog(@"title:%@",title.text);
            title.font=[UIFont systemFontOfSize:15];
            if(i==0)
                title.backgroundColor=[UIColor colorWithWhite:0.96 alpha:1];
            else
                title.backgroundColor=[UIColor whiteColor];
            title.textAlignment=NSTextAlignmentCenter;
            title.textColor=[UIColor colorWithWhite:0.46 alpha:1];
            [_mainTableView addSubview:title];
            
            
            title.tag=i*100+j+1;
            [self setEdge:self firstView:title secondView:_productTotalLabel attr:NSLayoutAttributeHeight constant:0];
            if(i==0)
            {
                [self setEdge:_mainTableView firstView:title secondView:_mainTableView attr:NSLayoutAttributeTop constant:0];
            }
            else
            {
                [_mainTableView addConstraint:[NSLayoutConstraint constraintWithItem:[_mainTableView viewWithTag:(i-1)*100+j+1] attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:title attribute:NSLayoutAttributeTop multiplier:1.0 constant:-1]];
            }
            
            
            
            if(j==0)
            {
                [self setEdge:_mainTableView firstView:title secondView:_mainTableView attr:NSLayoutAttributeLeft constant:0];
            }
            else
            {
                [_mainTableView addConstraint:[NSLayoutConstraint constraintWithItem:[_mainTableView viewWithTag:i*100+(j-1)+1] attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:title attribute:NSLayoutAttributeLeft multiplier:1.0 constant:-1]];
            }
            
            
            if(i!=0 || j!=0)
            {
                
                [self setEdge:self firstView:title secondView:[_mainTableView viewWithTag:1] attr:NSLayoutAttributeWidth constant:0];
            }
            
            
            if(i==data.count-1)
            {
                
                [self setEdge:_mainTableView firstView:title secondView:_mainTableView attr:NSLayoutAttributeBottom constant:0];
            }
            
            if(j==rowData.count-1)
            {
                [self setEdge:_mainTableView firstView:title secondView:_mainTableView attr:NSLayoutAttributeRight constant:0];
            }
            
            
        }
    }
}

//自动布局快速添加
- (void)setEdge:(UIView*)supperView firstView:(UIView*)firstView secondView:(UIView*)secondView attr:(NSLayoutAttribute)attr constant:(CGFloat)constant
{
    if(secondView==nil)
        [supperView addConstraint:[NSLayoutConstraint constraintWithItem:firstView attribute:attr relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:constant]];
    else [supperView addConstraint:[NSLayoutConstraint constraintWithItem:firstView attribute:attr relatedBy:NSLayoutRelationEqual toItem:secondView attribute:attr multiplier:1.0 constant:constant]];
}

@end
